﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		LowercasingRoute.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System.Web;
using System.Web.Routing;

namespace Dexter.Web.Mvc.Routing.Routes {
	public class LowercasingRoute : RouteBase {
		readonly RouteBase inner;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "LowercasingRoute" /> class.
		/// </summary>
		/// <param name = "routeToWrap">The route to wrap.</param>
		public LowercasingRoute ( RouteBase routeToWrap ) {
			inner = routeToWrap;
		}

		/// <summary>
		/// 	When overridden in a derived class, returns route information about the request.
		/// </summary>
		/// <param name = "httpContext">An object that encapsulates information about the HTTP request.</param>
		/// <returns>
		/// 	An object that contains the values from the route definition if the route matches the current request, or null if the route does not match the request.
		/// </returns>
		public override RouteData GetRouteData ( HttpContextBase httpContext ) {
			return inner.GetRouteData ( httpContext );
		}

		/// <summary>
		/// 	When overridden in a derived class, checks whether the route matches the specified values, and if so, generates a URL and retrieves information about the route.
		/// </summary>
		/// <param name = "requestContext">An object that encapsulates information about the requested route.</param>
		/// <param name = "values">An object that contains the parameters for a route.</param>
		/// <returns>
		/// 	An object that contains the generated URL and information about the route, or null if the route does not match <paramref name = "values" />.
		/// </returns>
		public override VirtualPathData GetVirtualPath ( RequestContext requestContext , RouteValueDictionary values ) {
			VirtualPathData result = inner.GetVirtualPath ( requestContext , values );
			if ( result != null && result.VirtualPath != null ) {
				result.VirtualPath = result.VirtualPath.ToLowerInvariant ( );
			}
			return result;
		}
	}
}
